/* eslint-disable no-unused-vars */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-shadow */
import { reactive } from 'vue'
import { FenceClass } from '@/store/fence'
import { PillarClass } from '@/store/pillar'
import { DoorClass } from '@/store/door'

export interface IDrawingNode {
  id: string

  points: number[]

  angle: number

  height: number

  width: number

  color: string
}

export class DrawingNode implements IDrawingNode {
  id: string

  points: number[]

  height: number

  width: number

  color: string

  angle: number

  constructor(
    id: string,
    points: number[],
    angle: number,
    height: number,
    width: number,
    color: string
  ) {
    this.id = id
    this.points = points
    this.angle = angle
    this.height = height
    this.width = width
    this.color = color
  }
}

export interface IDrawing {
  fences: Map<string, object>
  pillars: Map<string, object>
  doors: Map<string, object>
  history: Map<string, string[]>
}

export const Drawing: IDrawing = {
  fences: new Map(),
  pillars: new Map(),
  doors: new Map(),
  history: new Map()
}

export function createDrawing() {
  return reactive(Drawing)
}

export function createDrawingAction(Drawing: IDrawing) {
  return {
    setFence(id: string, fence: FenceClass) {
      Drawing.fences.set(id, fence)
    },
    setPillar(id: string, pillar: PillarClass) {
      Drawing.pillars.set(id, pillar)
    },
    setDoor(id: string, door: DoorClass) {
      Drawing.doors.set(id, door)
    },
    setHistory(id: string, children: string[]) {
      Drawing.history.set(id, children)
    },
    removeFence(id: string) {
      Drawing.fences.delete(id)
    },
    removePillar(id: string) {
      Drawing.pillars.delete(id)
    },
    removeDoor(id: string) {
      Drawing.doors.delete(id)
    },
    removeHistory(id: string) {
      Drawing.history.delete(id)
      if (id.search('fence') !== -1) {
        for (const [key, fence] of Drawing.fences) {
          if (key.search(id) !== -1) {
            Drawing.fences.delete(key)
          }
        }
      } else if (id.search('door') !== -1) {
        for (const [key, fence] of Drawing.doors) {
          if (key.search(id) !== -1) {
            Drawing.doors.delete(key)
          }
        }
      }
      for (const [key, fence] of Drawing.pillars) {
        if (key.search(id) !== -1) {
          Drawing.pillars.delete(key)
        }
      }
    },
    cleanAll() {
      Drawing.fences.clear()
      Drawing.pillars.clear()
      Drawing.doors.clear()
      Drawing.history.clear()
    }
  }
}
